<template>
  <div class="like" v-loading="routePraiseLoading">
    <div v-if="likeList.length > 0">
      <div
        class="route-item"
        v-for="item in likeList"
        @click="routeDetail(item)"
        :key="item.id"
      >
        <div class="cover">
          <img :src="item.cover" alt="" />
        </div>
        <div class="content">
          <div class="body">
            <div class="title">
              {{ item.name }}
            </div>
            <div class="user-info">
              <div class="avatar">
                <img :src="item.avatar || defaultAvatar" alt="" />
              </div>
              <span>{{ item.nickName }} </span>
            </div>
          </div>
          <div class="footer">
            <span>
              <i class="el-icon-chat-dot-square"> </i>
              {{ item.remarkCount }}
            </span>
            <span><i class="el-icon-view"></i>{{ item.views }}</span>
            <span>
              <svg
                width="13"
                height="13"
                viewBox="0 0 48 48"
                fill="rgb(250, 40, 40)"
                xmlns="http://www.w3.org/2000/svg"
              >
                <path
                  d="M15 8C8.92487 8 4 12.9249 4 19C4 30 17 40 24 42.3262C31 40 44 30 44 19C44 12.9249 39.0751 8 33 8C29.2797 8 25.9907 9.8469 24 12.6738C22.0093 9.8469 18.7203 8 15 8Z"
                  fill="none"
                  stroke="#999"
                  stroke-width="4"
                  stroke-linecap="round"
                  stroke-linejoin="round"
                />
              </svg>
              {{ item.praise }}
            </span>
          </div>
        </div>
      </div>
      <!-- 分页 -->
      <el-pagination
        v-show="likeList.length > 0"
        layout="prev, pager, next"
        :total="total"
        :current-page="currentPage"
        :page-size="pageSize"
        @current-change="handleCurrentChange"
        :page-count="7"
        style="text-align: right; margin-top: 24px"
      >
      </el-pagination>
    </div>
    <div class="empty" v-else>
      <el-empty description="暂无数据"></el-empty>
    </div>
  </div>
</template>

<script>
import defaultAvatar from "@/assets/defaultAvatar.png";
import { addRouteViews, getRoutePraiseList } from "@/api/user/route";
import userModule from "@/utils/userInfo";

export default {
  name: "Like",
  data() {
    return {
      defaultAvatar,
      likeList: [],
      total: 0,
      page: 1,
      pageSize: 10,
      currentPage: 1,
      routePraiseLoading: false,
    };
  },
  created() {
    this.getRoutePraiseList();
  },
  methods: {
    // 用户线路点赞查询
    async getRoutePraiseList() {
      try {
        this.routePraiseLoading = true;
        const res = await getRoutePraiseList({
          page: this.page,
          pageSize: this.pageSize,
          userId: userModule.getUser().id,
        });
        this.likeList = res.data.records;
        this.total = res.data.total;
      } catch {
        this.$message.error("获取失败");
      } finally {
        this.routePraiseLoading = false;
      }
    },
    // 分页
    handleCurrentChange(page) {
      this.page = page;
      this.currentPage = page;
      this.getRoutePraiseList();
    },
    // 查看详情
    async routeDetail(item) {
      // 新增访问量
      item.views = item.views + 1;
      const res = await addRouteViews({ id: item.routeId, views: item.views });
      this.$router.push(`/routeDetail/${item.routeId}`);
    },
  },
};
</script>

<style lang="less" scoped>
.like {
  height: 100%;
  padding-top: 2px;
  box-shadow: 0 0 6px #ccc;
  .route-item {
    display: flex;
    margin-bottom: 10px;
    padding: 20px;
    height: 140px;
    border-bottom: 1px solid #ddd;
    cursor: pointer;
    &:hover {
      .content {
        .title {
          color: rgb(255, 160, 0);
        }
      }
    }
    .cover {
      width: 180px;
      height: 100px;
      border-radius: 8px;
      img {
        width: 100%;
        height: 100%;
        border-radius: inherit;
      }
    }
    .content {
      flex: 1;
      margin: 0 16px;
      .body {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
        height: 90%;
        text-align: right;
        .title {
          width: 400px;
          font-size: 19px;
          font-weight: 600;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          transition: all 0.2s;
        }
        .user-info {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          margin: 6px 0;
          .avatar {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            margin-right: 8px;
            img {
              width: 100%;
              height: 100%;
              border-radius: inherit;
            }
          }
        }
      }
      .footer {
        height: 10%;
        font-size: 15px;
        color: #999;
        text-align: right;
        span {
          margin-left: 20px;
          i {
            margin-right: 4px;
          }
        }
      }
    }
  }
  .empty {
    height: 100%;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
  }
}
</style>